package com.live.service.impl;

import com.common.constant.enums.CommonStatus;
import com.common.constant.strings.SuccessMsg;
import com.common.entity.live.TbLive;
import com.common.response.CommonResponse;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.live.mapper.LiveMapper;
import com.live.service.ILiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author LiJinYang
 * @version 1.0
 * @email 283006176@qq.com
 * @date 2021/08/11 9:26
 */
@Service
public class LiveServiceImpl implements ILiveService {
    @Autowired
    private LiveMapper liveMapper;

    /**
     * 分页，模糊
     * @param tbLive
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo findPage(TbLive tbLive, int page, int size) {
        PageHelper.startPage(page,size);
        List<TbLive> list = liveMapper.findPage(tbLive);
        return new PageInfo(list);
    }

    /**
     * 修改直播状态
     * @param id
     * @param status
     */
    @Override
    public CommonResponse updateStatusById(Long id, String status) {
        //做修改之前要先计算一下直播时长
        TbLive live = liveMapper.findOne(id);
        long diff = System.currentTimeMillis()-live.getStartTime().getTime();
        long days = diff / (1000 * 60 * 60 * 24);
        long hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60);
        long minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);
        String liveTime = hours+"小时"+minutes+"分";
        live.setStatus(status);
        live.setLiveTime(liveTime);
        live.setEndTime(new Date());
        liveMapper.updateStatusById(live);
        return new CommonResponse(CommonStatus.SUCCESS.getCode(), SuccessMsg.SUCCESS_CODE_200);
    }

    /**
     * 查询单个直播
     * @param id
     * @return
     */
    @Override
    public TbLive findOne(Long id) {
        return liveMapper.findOne(id);
    }
}
